﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System;

public class ProjectNote : EditorWindow
{
    private Vector2 scrollPos;
    private string contentText = "";
    private DateTime lastWriteTime = DateTime.Now;
    private string fileName;

    public static void ShowWindow()
    {
        EditorWindow window = EditorWindow.GetWindow<ProjectNote>();
        window.position = new Rect(100, 100, 500, 400);
        window.titleContent = new GUIContent("工程便签");
        window.Show();
    }

    void Awake()
    {
        fileName = Application.dataPath + "/Editor/Note.txt";
        if (!File.Exists(fileName))
        {
            File.CreateText(fileName);
        }

        FileInfo info = new FileInfo(fileName);
        lastWriteTime = info.LastWriteTime;


        using (StreamReader sr = info.OpenText())
        {
            contentText = sr.ReadToEnd();
        }
    }

    void OnGUI()
    {
        GUILayout.Space(10);
        EditorTools.Label("工程便签", Color.yellow, 20, TextAnchor.MiddleCenter);
        GUILayout.Space(2);
        EditorTools.Label("上次修改时间：" + lastWriteTime.ToLocalTime(), Color.green, 12, TextAnchor.MiddleCenter);
        GUILayout.Space(6);

        scrollPos = GUILayout.BeginScrollView(scrollPos, false, false);
        {
            contentText = GUILayout.TextArea(contentText, GUILayout.MinHeight(350), GUILayout.MinWidth(450));
        }

        GUILayout.EndScrollView();

        GUILayout.Space(2);
        EditorTools.Label("小贴士： 支持 Control+S、Control+V、Control+C（全部复制） 快捷键操作，请多按几次，确保执行成功！\n关闭窗口自动保存", Color.white, 10, TextAnchor.MiddleCenter);
        GUILayout.Space(2);

        GUILayout.BeginHorizontal();

        //if (GUILayout.Button("粘贴"))
        //{
        //    contentText += GUIUtility.systemCopyBuffer;
        //}

        //if (GUILayout.Button("保存"))
        //{
        //    Save();
        //}

        GUILayout.EndHorizontal();

        SetCurrent();
    }

    void OnDestroy()
    {
        Save();
    }

    void Save()
    {
        using (FileStream fs = new FileStream(fileName, FileMode.Truncate))
        {
            using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
            {
                sw.Write(contentText);
            }
        }
    }

    /// <summary> 设置快捷操作 </summary>
    void SetCurrent()
    {
        Event e = Event.current;
        if (e.isKey)
        {
            bool EventDown = (e.modifiers & EventModifiers.Control) != 0;
            switch (e.keyCode)
            {
                case KeyCode.S:
                    if (EventDown)
                    {
                        e.Use();
                        Save();
                    }
                    break;
                //case KeyCode.A:
                //    if (EventDown)
                //    {
                //        e.Use();
                //        TextEditor te = new TextEditor();
                //        te.text = contentText;
                //        te.OnFocus();
                //    }
                //    break;
                case KeyCode.C:
                    if (EventDown)
                    {
                        e.Use();
                        TextEditor te = new TextEditor();
                        te.text = contentText;
                        te.OnFocus();
                        te.Copy();
                    }
                    break;
                case KeyCode.V:
                    if (EventDown)
                    {
                        e.Use();
                        contentText += GUIUtility.systemCopyBuffer; // 粘贴
                        GUIUtility.systemCopyBuffer = null; // 粘贴后清空粘贴板 
                    }
                    break;
            }
        }
    }
}
